import 'package:flutter/material.dart';
import 'package:flutter_enjoy_plus/pages/home/index.dart';
import 'package:flutter_enjoy_plus/pages/mine/index.dart';
import 'package:flutter_enjoy_plus/utils/EventBus.dart';
import 'package:flutter_enjoy_plus/utils/TokenManager.dart';

class TabsPage extends StatefulWidget {
  const TabsPage({super.key});

  @override
  _TabsPageState createState() => _TabsPageState();
}

class _TabsPageState extends State<TabsPage> {
  List tabsList = [
    {
      'label': '首页',
      'icon': 'assets/tabs/home_default.png',
      'activeIcon': 'assets/tabs/home_active.png'
    },
    {
      'label': '我的',
      'icon': 'assets/tabs/my_default.png',
      'activeIcon': 'assets/tabs/my_active.png'
    }
  ];
  int activeIndex = 0;
  updataactiveIndex(int index) {
    setState(() {
      activeIndex = index;
    });
  }

  // gettab() {
  //   if (activeIndex == 0) {
  //     return const HomePage();
  //   } else if (activeIndex == 1) {
  //     return MinePage(
  //       currentIndex: activeIndex,
  //     );
  //   }
  // }

  List<BottomNavigationBarItem> getTabsBar() {
    List<BottomNavigationBarItem> list = [];
    for (var i = 0; i < tabsList.length; i++) {
      list.add(
        BottomNavigationBarItem(
            label: tabsList[i]["label"],
            icon: Image.asset(
              tabsList[i]["icon"],
              width: 30,
              height: 30,
            ),
            activeIcon: Image.asset(
              tabsList[i]["activeIcon"],
              width: 30,
              height: 30,
            )),
      );
    }
    return list;
  }

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    tokenManager.init();
    eventBus.on<LogoutEvent>().listen((event) {
      Navigator.pushNamed(context, '/login');
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: SafeArea(
        child: IndexedStack(
          index: activeIndex,
          children: [
            const HomePage(),
            MinePage(
                currentIndex: activeIndex, updataactiveIndex: updataactiveIndex)
          ],
        ),
      ),
      bottomNavigationBar: BottomNavigationBar(
        type: BottomNavigationBarType.fixed,
        currentIndex: activeIndex,
        selectedItemColor: const Color.fromRGBO(255, 182, 193, 1.0),
        unselectedItemColor: Colors.black,
        items: getTabsBar(),
        onTap: (int index) {
          setState(() {
            activeIndex = index;
          });
        },
      ),
    );
  }
}
